<template>
  <a-modal :width="500" v-model:visible="showSetProgressDialog" title="设置采购进度" title-align="start" :mask-closable="false" :on-before-ok="submitData" :ok-loading="submitLoading" @cancel="closedDialog">
    <a-radio-group direction="vertical" v-model="setType">
      <a-radio :value="1">绑定采购订的状态作为采购进度</a-radio>
      <a-radio :value="2">手动设置</a-radio>
    </a-radio-group>
    <a-space style="margin: 10px 25px" v-show="setType === 2">
      <a-select style="width: 200px" placeholder="请选择" v-model="purchaseStatus">
        <a-option v-for="(item, i) in purchaseStatusList" :key="i" :value="item.purchaseStatus" :label="item.purchaseStatus" />
      </a-select>
      <a-button type="text" size="medium" @click="showSetPurchaseStatus = true">新增项目进度</a-button>
    </a-space>
  </a-modal>
  <SetPurchaseStatus v-if="showSetPurchaseStatus" :isShow="showSetPurchaseStatus" :list="purchaseStatusList" @closed="showSetPurchaseStatus = false" @success="getStatusList" />
</template>
<script lang="ts" setup>
  import { ref } from 'vue'
  import { Message } from '@arco-design/web-vue'
  import { purchasePlanItemSetPurchaseStatus, findPurchaseStatusPage } from '@/api/purchaseApplication'
  import SetPurchaseStatus from './setPurchaseStatus.vue'

  const emit = defineEmits(['closed', 'success'])
  const props = defineProps({
    isShow: {
      type: Boolean,
      default: false,
    },
    list: {
      type: Array,
      default: () => [],
    },
  })

  const showSetProgressDialog = ref(props.isShow)
  const setType = ref(1)
  const purchaseStatus = ref()
  const submitLoading = ref(false)
  const showSetPurchaseStatus = ref(false)
  const purchaseStatusList = ref<Array<any>>([])

  // 获取状态列表
  async function getStatusList() {
    console.log(12222)
    const res: any = await findPurchaseStatusPage({
      pageNum: 1,
      pageSize: 1000,
    })
    purchaseStatusList.value = res?.data?.records || []
  }
  getStatusList()

  // 关闭
  function closedDialog() {
    showSetProgressDialog.value = false
    emit('closed')
  }
  // 提交
  async function submitData() {
    if (setType.value === 2 && !purchaseStatus.value) {
      Message.error('请选择项目进度')
      return false
    }
    let _params: any = {
      setType: setType.value,
      ids: props.list.map((el: any) => el.id),
    }
    if (setType.value === 2) {
      _params.purchaseStatus = purchaseStatus.value
    }
    const res: any = await purchasePlanItemSetPurchaseStatus(_params)
    if (!res) return false
    Message.success('设置成功')
    emit('success')
    closedDialog()

    return true
  }
</script>
<style lang="less" scoped></style>
